<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Model;

class RecruitJobCate extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    /**
     * 配置管理列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getJobCateList($params = [])
    {
        $data["data"] = self::field("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text ")
            ->where(function ($query) use ($params){
                $query->whereNull('delete_time');
                if(!empty($params['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$params['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($params["word"])) $query->where("job_name",'like', '%'.$params["word"].'%');
                if(!empty($params["type"])) $query->where("type", $params["type"]);
            })
            ->order('id','desc')
            ->page($params['page'], $params['limit'])
            ->select()->toArray();

        $data["count"] = self::where(function ($query) use ($params){
                $query->whereNull('delete_time');
                if(!empty($params['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$params['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($params["word"])) $query->where("job_name",'like', '%'.$params["word"].'%');
                if(!empty($params["type"])) $query->where("type", $params["type"]);
            })
            ->count();

        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val){
                $val["type_text"] = self::getTypeText($val['type']);
            }
        }

        return $data;
    }


    public static function getTypeText($type = '')
    {
        $arr = [
            1 => "工作经验",
            2 => "学历",
            3 => "求职状态",
            4 => "薪资",
            5 => "标签",
        ];
        return $arr[$type] ?? '';
    }


    /**
     * 配置管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addJobCate($param = [])
    {
        $add = [
            "job_name"      => $param["job_name"],
            "type"          => $param["type"] ?? '',
            "sort"          => $param["sort"] ?? '',
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 配置管理列表 - 编辑
     * @param $param
     * @return RecruitLanguage
     */
    public static function updateJobCate($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["job_name"]))      $update["job_name"] = $param["job_name"];
        if(!empty($param["type"]))          $update["type"] = $param["type"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"]; //类型

        return self::where("id", $param["id"])->update($update);
    }




    /**
     * 配置管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableJobCate($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }




    public static function getJobCateData()
    {
        $arr = [
            ["cid" => 1, "cname" => "工作经验"],
            ["cid" => 2, "cname" => "学历"],
            ["cid" => 3, "cname" => "求职状态"],
            ["cid" => 4, "cname" => "薪资"],
            ["cid" => 5, "cname" => "标签"],
        ];
        return $arr;
    }











}
